/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Anthony
 */

//importing needed resources for GUI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;

class puzzleGUI extends JPanel {
    //starting
    public static void main(String[] args) {
        JFrame window = new JFrame("CP1300 A1");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(new puzzleGUI());
        window.pack();
        window.setVisible(true);
        window.setResizable(true);
    }
    private puzzleBoard puzzleBoard;
    private puzzlePieces puzzlePieces = new puzzlePieces();
    JLabel timeLabel;
    JLabel moveLabel;
    int counter;
    Timer timer;
    int moveCount;

    public puzzleGUI() {
        //values for the move counter and timer
        counter = 0;
        moveCount = 0;

        timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                ++counter;
                timeLabel.setText("Time: " + counter);
            }
        });
        //starting the timer
        timer.start();

        //adding labels
        timeLabel = new JLabel("?", JLabel.CENTER);
        timeLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 9));

        moveLabel = new JLabel("?", JLabel.CENTER);
        moveLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 9));
        moveLabel.setText("Moves: " + moveCount);
        //Making the stuff for the top panel
        JButton scrambleButton = new JButton("3x3");
        JButton scrambleMedium = new JButton("4x4");
        JButton scrambleHard = new JButton("5x5");
        JButton scrambleSix = new JButton("6x6");
        //functions buttons will call
        scrambleButton.addActionListener(new setThree());
        scrambleMedium.addActionListener(new setFour());
        scrambleHard.addActionListener(new setFive());
        scrambleSix.addActionListener(new setSix());
        //font for the buttons
        scrambleButton.setFont(new Font("Comic Sans MS", Font.PLAIN, 9));
        scrambleMedium.setFont(new Font("Comic Sans MS", Font.PLAIN, 9));
        scrambleHard.setFont(new Font("Comic Sans MS", Font.PLAIN, 9));
        scrambleSix.setFont(new Font("Comic Sans MS", Font.PLAIN, 9));

        //This doesn't even do anything with the name that they type in but the assignment asks for it
        //JTextField playerName = new JTextField("Enter name", 10);
        //playerName.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        topPanel.add(moveLabel);
        topPanel.add(scrambleButton);
        topPanel.add(scrambleMedium);
        topPanel.add(scrambleHard);
        topPanel.add(scrambleSix);
        //topPanel.add(playerName);
        topPanel.add(timeLabel);
        puzzleBoard = new puzzleBoard();

        this.setLayout(new BorderLayout());
        this.add(topPanel, BorderLayout.NORTH);
        this.add(puzzleBoard, BorderLayout.CENTER);

    }

    class puzzleBoard extends JPanel implements MouseListener {

        //These can be changed in the second part of the assignment but only here at the moment
        public int ROWS = 4;
        public int COLS = 4;
        static final int buttonSquare = 75;
        private Font font;

        //Style        
        public puzzleBoard() {
            //Comic Sans is the best font so it's used here
            font = new Font("Comic Sans MS", Font.BOLD, buttonSquare / 2);
            this.setPreferredSize(
                    new Dimension(buttonSquare * COLS - 10, buttonSquare * ROWS - 10));
            this.setBackground(new Color(153, 217, 234));
            this.addMouseListener(this);
        }

        public void paintComponent(Graphics button) {
            //This is so I don't need to do it over and over
            super.paintComponent(button);
            for (int r = 0; r < ROWS; r++) {
                for (int c = 0; c < COLS; c++) {
                    int x = c * buttonSquare;
                    int y = r * buttonSquare;
                    String btnText = puzzlePieces.getButtonNum(r, c);
                    //The "buttons" are just painted rectangles
                    if (btnText != null) {
                        button.setColor(new Color(112, 146, 190));
                        button.fillRect(x, y, buttonSquare - 3, buttonSquare - 3);
                        button.setColor(new Color(255, 174, 201));
                        button.setFont(font);
                        button.drawString(btnText, x + 15, y + 50);
                    }
                }
            }
        }

        public void mousePressed(MouseEvent e) {
            int col = e.getX() / buttonSquare;
            int row = e.getY() / buttonSquare;
            //When mouse is pressed adds 1 to moves
            ++moveCount;
            moveLabel.setText("Moves: " + moveCount);
            //Checks the boolean in puzzlePieces
            if (!puzzlePieces.moveTile(row, col)) {
                //If it can't move tiles it subtracts the 1 added to moves from mousepress and beeps.
                --moveCount;
                moveLabel.setText("Moves: " + moveCount);
                Toolkit.getDefaultToolkit().beep();
            }
            this.repaint();
        }

        //Without these I need to add exceptions and junk.
        public void mouseClicked(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

    //resets game, moves, and timer
    //Setting grid to 3x3
    public class setThree implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            puzzleBoard.ROWS = 3;
            puzzleBoard.COLS = 3;
            puzzlePieces.ROWS = 3;
            puzzlePieces.COLS = 3;
            puzzlePieces.reset();
            puzzleBoard.repaint();
            counter = 0;
            moveCount = 0;
            timeLabel.setText("Time: " + timer);
            moveLabel.setText("Moves: " + moveCount);
        }
    }
    //Setting grid to 4x4
    public class setFour implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            puzzleBoard.ROWS = 4;
            puzzleBoard.COLS = 4;
            puzzlePieces.ROWS = 4;
            puzzlePieces.COLS = 4;
            puzzlePieces.reset();
            puzzleBoard.repaint();
            counter = 0;
            moveCount = 0;
            timeLabel.setText("Time: " + timer);
            moveLabel.setText("Moves: " + moveCount);
        }
    }
    //Setting grid to 5x5
    public class setFive implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            puzzleBoard.ROWS = 5;
            puzzleBoard.COLS = 5;
            puzzlePieces.ROWS = 5;
            puzzlePieces.COLS = 5;
           // puzzlePieces.reset();
            puzzleBoard.repaint();
            counter = 0;
            moveCount = 0;
            timeLabel.setText("Time: " + timer);
            moveLabel.setText("Moves: " + moveCount);
        }
    }
    //Setting grid to 6x6
    public class setSix implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            puzzleBoard.ROWS = 6;
            puzzleBoard.COLS = 6;
            puzzlePieces.ROWS = 6;
            puzzlePieces.COLS = 6;
            puzzlePieces.reset();
            puzzleBoard.repaint();
            counter = 0;
            moveCount = 0;
            timeLabel.setText("Time: " + timer);
            moveLabel.setText("Moves: " + moveCount);
        }
    }
}